// pages/addIPay/addIPay.js
var util = require('../../utils/util.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    leibie: '肉菜吃饭',
    mingcheng: '',
    shouzhi: '-',
    jine: '',
    jilurenID: wx.getStorageSync("openid"),
    jiluren: (app.globalData.userInfo == undefined || app.globalData.userInfo == {} || app.globalData.userInfo == "") ? "" : app.globalData.userInfo.nickName,
    jilushijian: '',
    jlr_array: ['bing', 'yan'],
    jlr_index: 0,
    jlr_df: '请选择',
    lb_items: [{
        name: '肉菜吃饭',
        value: '肉菜吃饭',
        checked: 'true'
      },
      {
        name: '水果',
        value: '水果'
      },
      {
        name: '出行',
        value: '出行'
      },
      {
        name: '生活用品',
        value: '生活用品'
      },
      {
        name: '水电物业',
        value: '水电物业'
      },
      {
        name: '还款',
        value: '还款'
      },
      {
        name: '文具玩具',
        value: '文具玩具'
      },
      {
        name: '看病或药',
        value: '看病或药'
      },
      {
        name: '其他',
        value: '其他'
      }
    ],
    sz_items: [
	  {
        name: '-',
        value: '支出',
        checked: 'true'
      },
      {
        name: '+',
        value: '收入'
      }
    ],
    // date: (new Date()).toLocaleDateString().replace(new RegExp('/', 'g'), '-')
    date: util.formatDate(new Date())
  },


  lb_checkboxChange: function (e) {
    var s = e.detail.value;
    var lb = '';
    for (var index in s) {
      lb += s[index] + ','
    };
    this.data.leibie = lb.substring(0, lb.length - 1);
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
  },

  sz_radioChange: function (e) {
    this.setData({
      shouzhi: e.detail.value
    });
    console.log(this.data.shouzhi);
    console.log('radio发生change事件，携带value值为：', e.detail.value);
  },

  jlsj_bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      date: e.detail.value
    })
  },

  bindJlrChange: function (e) {
    console.log('bindJlrChange发送选择改变，携带值为', e.detail.value);
    let i = e.detail.value;
    let array = this.data.jlr_array;
    this.setData({
      jlr_index: i,
      jlr_df: array[i],
      jiluren: array[i]
    });
  },

  onCancel: function () {
    wx.navigateBack();
  },

  input_mingcheng: function (e) {
    this.setData({
      mingcheng: e.detail.value
    });
  },

  input_jine: function (e) {
    this.setData({
      jine: e.detail.value
    });
  },

  input_jiluren: function (e) {
    this.setData({
      jiluren: e.detail.value
    });
  },

  input_jilushijian: function (e) {
    this.setData({
      jilushijian: e.detail.value
    });
  },

  isNumber: function (val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
      return true;
    } else {
      return false;
    }
  },

  onConfirm: function () {

    //检查金额是否数字
    var that = this;
    if (!that.isNumber(this.data.jine)) {
      wx.showToast({
        title: '金额填写不对',
        icon: 'none',
        duration: 1000
      });
      return;
    }

    var postObj = {
      "ID": Date.parse(new Date()) / 1000,
      "CATEGORY": this.data.leibie,
      "IN_OUT": this.data.shouzhi,
      "PRICE": this.data.shouzhi == '-' ? '-' + this.data.jine : this.data.jine,
      "CONTENT": this.data.mingcheng,
      "CREATOR_ID": this.data.jilurenID,
      "CREATOR": this.data.jiluren,
      "RECORD_TIME": this.data.date
    };
    console.log(JSON.stringify(postObj));
    debugger;
    wx.request({
      url: wx.getStorageSync("sURL"),
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        url: '/addExp',
        postdata: JSON.stringify(postObj)
      },
      method: 'POST',
      success: res => {
        console.log(res);
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          duration: 500,
          mask: true,
          success: function () {
            setTimeout(function () {
              //要延时执行的代码
              wx.switchTab({
                url: '../iPay/iPay'
              });
            }, 500) //延迟时间
          }
        });
      },
      fail: res => {
        console.log(res);
        wx.showToast({
          title: '添加失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this;
    that.setData({
      userInfo: app.globalData.userInfo
    });

    if (that.data.userInfo == undefined || that.data.userInfo == {} || that.data.userInfo == "") {
      wx.showToast({
        title: '请先在主页点击授权',
        icon: 'none',
        duration: 3000
      });
    } else {
      that.setData({
        jilurenID: wx.getStorageSync("openid"),
        jiluren: that.data.userInfo.nickName,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})